package com.teamb6.shop.service.impl;

import com.teamb6.shop.domain.Category;
import com.teamb6.shop.domain.CategoryExample;
import com.teamb6.shop.mapper.CategoryMapper;
import com.teamb6.shop.service.CateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CateServiceImpl implements CateService {
    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public List<Category> listCategory(String keyword) {
        keyword="%"+keyword+"%";
        CategoryExample example = new CategoryExample();
        example.or().andCatenameLike(keyword);
        List<Category> categories = categoryMapper.selectByExample(example);
        return categories;
    }

    //添加分类列表
    @Override
    public List<Category> listCategoryAndGoodNumber() {
        return categoryMapper.selectAllAndGoodNumber();
    }

    //添加分类列表
    @Override
    public int addCategory(Category category) {
        return categoryMapper.insert(category);
    }

    //查询商品分类
    @Override
    public List<Category> selectByExample(CategoryExample example) {
        return categoryMapper.selectByExample(example);
    }

    //删除单行商品分类
    @Override
    public int deleteCategoryByPrimaryKey(Integer id) {
        return categoryMapper.deleteByPrimaryKey(id);
    }

    //删除多行商品分类
    @Override
    public int multiDelCategories(String ids) {
        CategoryExample example = new CategoryExample();
        List<String> list = Arrays.asList(ids.split(","));
        List<Integer> ilist = list.stream().map(Integer::parseInt).collect(Collectors.toList());
        example.or().andCateidIn(ilist);
        return categoryMapper.deleteByExample(example);
    }

    //获取选中的商品分类信息
    @Override
    public Category findCategoryByPrimaryKey(Integer id) {
        return categoryMapper.selectByPrimaryKey(id);
    }

    //更新更改后的商品分类信息
    @Override
    public int updateCategory(Category category) {
        return categoryMapper.updateByPrimaryKeySelective(category);
    }


}
